<template>
  <div class="login">
    <div class="container">
      <div class="img-box">
        <img :src="src" alt="" />
      </div>
      <el-form
        ref="ruleFormRef"
        :model="ruleForm"
        :rules="rules"
        label-width="75px"
        class="demo-ruleForm"
        :size="formSize"
      >
        <el-form-item label="用户名" prop="username">
          <el-input v-model="ruleForm.username" />
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input v-model="ruleForm.password" type="password" />
        </el-form-item>
        <el-form-item class="btns">
          <el-button type="primary" @click="submit(ruleFormRef)"
            >登录</el-button
          >
          <el-button type="warning" @click="reset(ruleFormRef)">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script setup>
/*---------------------- 引入资源 ---------------------- */
import { ref, reactive } from "vue";
import { } from "element-plus";
import { error, success } from "@/utils/message";
import request from "@/utils/request";
import src from "@/assets/img/logo.png";
import router from "@/router";

/*---------------------- 声明绑定数据 ---------------------- */
// 表单元素大小
const formSize = ref("default");
//表单的引用
const ruleFormRef = ref();
//表单数据源
const ruleForm = reactive({
  username: "admin",
  password: "123456",
});
//表单验证规则
const rules = reactive({
  username: [
    { required: true, message: "请输入用户名", trigger: "blur" },
    { min: 3, max: 12, message: "请输入3-12位字符", trigger: "blur" },
  ],
  password: [
    { required: true, message: "请输入密码", trigger: "blur" },
    { min: 6, max: 12, message: "请输入6-12位字符", trigger: "blur" },
  ],
});

/*---------------------- 函数部分 ---------------------- */
//重置表单
let reset =(form)=>{
  form.resetFields()
}
//登录函数
async function login() {
  let opt = { url: "login", data: ruleForm, type: "post" };
  try {
    let { data: res } = await request(opt);
    if (res.meta.status !== 200) {
      return error(res.meta.msg);
    }
    success(res.meta.msg);
    sessionStorage.setItem("user-login", JSON.stringify(res));
    //跳转到 home
    router.push("/home");
  } catch (err) {
    console.log(err);
  }
}
//提交表单
function submit(formRef) {
  //表单验证
  formRef.validate((valid) => {
    if (!valid) {
      return error("请正确填写用户信息");
    }
    login();
  });
}
</script>

<style lang="scss" scoped>
.login {
  background-image: url("../assets/img/bg.gif");
  position: fixed;
  top: 0;
  left: 0;
  bottom: 0;
  right: 0;
  .container {
    width: 450px;
    height: 250px;
    background-color: #004f6e;
    padding: 20px;
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    border-radius: 5px;
    .img-box {
      position: absolute;
      top: 0;
      left: 50%;
      width: 150px;
      height: 150px;
      border-radius: 50%;
      background-color: #004f6e;
      border: 15px solid #698da0;
      transform: translate(-50%, -50%);
      img {
        display: block;
        width: 90px;
        position: absolute;
        top: 50%;
        left: 50%;
        transform: translate(-50%, -40%);
      }
    }
    .el-form {
      position: absolute;
      left: 0;
      bottom: 0;
      width: 100%;
      padding: 0 20px 0 10px;
      :deep(.el-form-item__label) {
        color: #fff;
      }
      .btns {
        :deep(.el-form-item__content) {
          display: flex;
          align-items: center;
          justify-content: flex-end;
        }
      }
    }
  }
}
</style>